<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>维护管理</el-breadcrumb-item>
                <el-breadcrumb-item>维修记录</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="RecordFormData"  class="demo-form-inline" >
                <el-form-item >
                    <el-input v-model="RecordFormData.carName" placeholder="请输入车辆名称"></el-input>
                </el-form-item>

                <el-form-item  >
                    <el-form-item >
                        <el-date-picker
                                v-model="RecordFormData.createtime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="date"
                                placeholder="日期">
                        </el-date-picker>

                    </el-form-item>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="recordId"
                            label="维修单号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="id"
                            label="车辆编号"
                            width="120">
                    </el-table-column>
                    <el-table-column
                            prop="tbCar.carName"
                            label="车辆名称"
                            width="150">
                    </el-table-column>
                    <el-table-column
                            prop="carImg"
                            label="汽车样式">
                        <template   slot-scope="scope">
                            <img :src="scope.row.carImg"  min-width="170" height="80" />
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="tbShop.name"
                            label="维修厂名称">
                    </el-table-column>
                    <el-table-column
                            prop="amount"
                            label="金额">
                    </el-table-column>

                    <el-table-column
                            prop="createtime"
                            label="建立时间">
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="建议">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>

        <!--弹出框-->
        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
        >
            <!--            model:表单数据-->
            <el-form  label-width="80px" :model="recordForm"  :rules="FormRules" ref="recordForm">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="recordForm.id" ></el-input>
                </el-form-item>

                <el-form-item label="车辆id" prop="carId">
                    <el-select v-model="recordForm.carId" placeholder="请选择车辆id" >
                        <el-option
                                v-for="itemsss in carArray"
                                :key="itemsss.id"
                                :label="itemsss.id"
                                :value="itemsss.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>


                <el-form-item label="维修厂" >
                    <el-select v-model="recordForm.maintainShopId" placeholder="请选择维修厂" >
                        <el-option
                                v-for="itemsss in shopArray"
                                :key="itemsss.id"
                                :label="itemsss.name"
                                :value="itemsss.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="汽车样式" prop="carImg">
                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:1000/maintain/upload"
                            :headers="headers"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrlLog" :src="imageUrlLog" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>


                <el-form-item label="金额"  placeholder="金额" prop="amount">
                    <el-input v-model="recordForm.amount"></el-input>
                </el-form-item>
                <el-form-item label="时间" >
                    <el-form-item >
                        <el-date-picker
                                v-model="recordForm.createtime"
                                type="date"
                                placeholder="日期">
                        </el-date-picker>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="建议"  placeholder="请给出建议">
                    <el-input v-model="recordForm.remark"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="formSubmit">确 定</el-button>
                <el-button @click="closeRecordForm()"  >取 消</el-button>

              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Record",
        data(){
            return {
                //提示框
                editDialogVisible:false,
                //条件搜索对象
                RecordFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                //添加表:
                recordForm:{},

                shopArray:[],

                carArray:[],
                //条件搜索对象
                shopFormData:{},
                //图片
                imageUrlLog:"",
                headers:{
                    token:sessionStorage.getItem("token")
                },


                FormRules:{
                    amount:[
                        {required:true,message:"金额不能为空",trigger:"blur"}
                        ],
                    carId:[
                        {required:true,message:"车辆id不能为空",trigger:"blur"}
                    ]
                }

            }
        },
        created() {
            this.selectRecordAll();
            this.selectListAll(); //维修厂下拉框
            this.selectCarList();//车辆id下拉
        },

        methods:{
            closeRecordForm(){
                this.editDialogVisible = false
                this.recordForm={}
            },

            //图片上传
            handleAvatarSuccess(res, file) {
                this.imageUrlLog =res.result.path;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isJPG && isLt2M;
            },

            insert(){
                this.editDialogVisible=true;
                this.imageUrlLog="";
                // this.$refs.recordForm.resetFields();
                this.recordForm={};
            },
            deleteById(id){
                var that=this;
                this.$http.delete(`record/delete/${id}`).then(function (returnResult) {
                    that.$message.success("删除成功");
                    that.selectRecordAll();
                })
            },
            selectById(id){

                var that=this;
                this.$http.get(`record/selectById/${id}`).then(function (returnResult) {
                    console.log(returnResult.data.result.tbRecord.carImg)
                    that.imageUrlLog=returnResult.data.result.tbRecord.carImg
                    that.editDialogVisible=true;
                    that.recordForm=returnResult.data.result.tbRecord
                })
                this.recordForm={};

            },
            formSubmit(){
                console.log(this.recordForm)
                var that=this;
                if(this.recordForm.id!=null){
                    this.recordForm.carImg=this.imageUrlLog
                    this.$http.put("record/update",this.recordForm).then(function (returnResult) {

                        that.editDialogVisible=false;
                        that.recordForm={};
                        that.selectRecordAll();
                    })
                }else {
                    this.recordForm.carImg=this.imageUrlLog
                    this.$http.post("record/insert", this.recordForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.recordForm={};
                        that.selectRecordAll();
                    })
                }
            },
            chaxun(){
                this.selectRecordAll();
            },
            selectRecordAll(){
                var that=this;
                this.$http.post(`/record/select/${this.currentPage}/${this.pageSize}`,this.RecordFormData).then(function (returnResult) {
                    console.log(returnResult)
                    that.tableData=returnResult.data.result.records;
                    that.total=returnResult.data.result.total;
                })
            },
            selectListAll(){
              var that=this;
                this.$http.get("/shop/selectListAll").then(function (returnResult) {
                    that.shopArray=returnResult.data.result.tbShops;
                    console.log(that.shopArray)
                })

            },
            //查询车辆id下拉
            selectCarList(){
                var that=this;
                this.$http.get("/record/selectCarList").then(function (returnResult) {
                    that.carArray=returnResult.data.result.tbCar;
                })

            },
            handleSizeChange(v){
                this.pageSize=v;
                this.selectRecordAll();
            },
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectRecordAll();
            }

        }
    }
</script>

<style>

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }


</style>